package com.atguigu.redis7.controller;

import com.atguigu.redis7.entities.Customer;
import com.atguigu.redis7.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Random;

/**
 * @版本： V5.1.0
 * @版权： Copyright @ 2025 广东广昱信息科技有限公司版权所有
 * @作者： 广东广昱信息科技有限公司
 * @日期： 2025-07-13 16:54
 */
@Api(tags = "客户Customer接口+布隆过滤器讲解")
@RestController
@Slf4j
public class CustomerController {
    @Resource
    private CustomerService customerService;


    @ApiOperation("数据库初始化2条Customer记录插入")
    @PostMapping("/customer/add")
    public void addCustomer(){
        for(int i = 0;i < 2;i++){
            Customer customer = new Customer();
            customer.setCname("customer"+i);
            customer.setAge(new Random().nextInt(30)+1);
            customer.setPhone("138000000"+i);
            customer.setSex((byte)new Random().nextInt(2));
            customer.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));
            customerService.addCustomer(customer);
        }
    }

    @ApiOperation("单个customer查询操作，按照customerid查询")
    @GetMapping("/customer/{id}")
    public Customer findCustomerById(@PathVariable Integer id){
        return customerService.findCustomerById(id);
    }
    @ApiOperation("BloomFilter，按照customerid查询")
    @GetMapping("/customerbloomfilter/{id}")
    public Customer findCustomerByIdWithBloomFilter(@PathVariable Integer id){
        return customerService.findCustomerByIdWithBloomFilter(id);
    }
}
